package teacher;

import com.tern.data.Entity;
import com.tern.db.DBCommand;
import com.tern.db.SqlHelper;
import com.tern.util.Convert;
import com.tern.util.Trace;
import com.tern.web.PageForm;
import com.tern.web.html;

public class StudentManagerPage  extends PageForm 
{
	private Entity entity;
	private int classid;
	public int getClassid() {
		return classid;
	}
	private int teacherID;
	public int getTeacherID() {
		return teacherID;
	}
	private String teacher;
	public String getTeacher() {
		return teacher;
	}
	private String classSelection;
	//////////////
	private String classCode;
	public String getClassCode() {
		return classCode;
	}
	private String startTimeString;
	public String getStartTimeString() {
		return startTimeString;
	}
	private String course;
	public String getCourse() {
		return course;
	}
	////////////////////
	public String getClassSelection() {
		if(classSelection==null){
			StringBuffer sel=new StringBuffer();
			classSelection=TeacherCommon.createSelection(teacherID,classid,sel);
			String[] selsStrings=sel.toString().split("--");
			classCode=selsStrings[0];
			course=selsStrings[1];
			startTimeString=selsStrings[2];
		}
		return classSelection;
	}
	final protected boolean onLoad() throws Exception
	{
		entity = Entity.createFromSchema("curricula");
		teacherID = Convert.parseInt(session.getAttribute("userID").toString());
		teacher=session.getAttribute("userName").toString();
		classid = Convert.parseInt(request.getParameter("classid"));
		try {
			if(classid<=0){
				String startTime = com.tern.util.Configuration.getInstance()
				.getValue("class.startTime");
				Entity classEntity=Entity.createFromSchema("class");
				classEntity.retriveFromDB("teacherID="+teacherID+" AND startTime='"+startTime+"'");
				if(classEntity.getRowsCount()>0){
					classid=classEntity.get(0).getInt("classID");
				}else{
					classEntity.retriveFromDB("teacherID="+teacherID+" order by classID desc");
					if(classEntity.getRowsCount()>0)
					classid=classEntity.get(0).getInt("classID");
				}
			}
			entity.retriveFromDB("classID=" + classid);
		} catch (Exception e) {
			Trace.out(Trace.Error, "StudentTestPage", e);
		}
		getClassSelection();
		return true;
	}
	public Entity getCurricula()
	{		
		return entity;
	}
	protected boolean onEvent(String eventName, String eventParams) {
		return true;
	}
}
